﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanTriHeThong.DanhMuc
{
    public class DMQuanHuyen : DBInteractionBase
    {
        public string MaQuanHuyen { get; set; }
        public string TenQuanHuyen { get; set; }
        public string MaTinhThanh { get; set; }

        public bool Insert(DMQuanHuyen obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMQuanHuyen_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaQuanHuyen", ConvertNvarcharToVarchar(obj.MaQuanHuyen)));
                    dbcmd.Parameters.Add(new SqlParameter("@TenQuanHuyen", obj.TenQuanHuyen));
                    dbcmd.Parameters.Add(new SqlParameter("@MaTinhThanh", obj.MaTinhThanh));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(DMQuanHuyen obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMQuanHuyen_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaQuanHuyen", obj.MaQuanHuyen));
                    dbcmd.Parameters.Add(new SqlParameter("@TenQuanHuyen", obj.TenQuanHuyen));
                    dbcmd.Parameters.Add(new SqlParameter("@MaTinhThanh", obj.MaTinhThanh));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMQuanHuyen_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaQuanHuyen", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable SelectById(string id)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMQuanHuyen_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaQuanHuyen", id));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMQuanHuyen_SelectById: " + ex.Message);
            }
        }

        public DataTable SelectAll(DMQuanHuyen obj, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMQuanHuyen_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaQuanHuyen", string.IsNullOrEmpty(obj.MaQuanHuyen) ? "" : obj.MaQuanHuyen));
                    dbcmd.Parameters.Add(new SqlParameter("@TenQuanHuyen", string.IsNullOrEmpty(obj.TenQuanHuyen) ? "" : obj.TenQuanHuyen));
                    dbcmd.Parameters.Add(new SqlParameter("@MaTinhThanh", string.IsNullOrEmpty(obj.MaTinhThanh) ? "" : obj.MaTinhThanh));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMQuanHuyen_SelectAll: " + ex.Message);
            }
        }
    }
}
